﻿//Copyright (c) 2010 William Liu, Enoch Ye
//William Liu <william.lyz@gmail.com>
//Enoch Ye <moonlight.enoch@gmail.com>

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using GBG_DBM.GlobalParameters;

namespace GBG_DBM.SysCheck
{
    /// <summary>
    /// This class is using to check the database management system integrity
    /// for example: the existence of database file, the right file path
    /// </summary>
    class AppCheck
    {
        #region FILEDS

        private static bool _isAppCheckPass = false;

        #endregion

        #region PROPERTIES

        public bool IsAppCheckPass { get { return _isAppCheckPass; } }

        #endregion

        #region CONSTRUCTORS
        #endregion

        #region METHODS

        public void CheckApp()
        {
            if (File.Exists(GlobalParams.DBPath) && Directory.Exists(GlobalParams.DefaultBackupPath()))
            {
                _isAppCheckPass = true;
            }
            else if (File.Exists(GlobalParams.DBPath) && !Directory.Exists(GlobalParams.DefaultBackupPath()))
            {
                try
                {
                    Directory.CreateDirectory(GlobalParams.DefaultBackupPath());
                    _isAppCheckPass = true;
                }
                catch (Exception _Ex)
                {
                    _isAppCheckPass = false;
                    MessageBox.Show("System cannot pass the application checking!" + "\nReason:" + _Ex.Message.ToString(), "Error",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error,
                                        MessageBoxDefaultButton.Button1);
                }
            }
            else if (!File.Exists(GlobalParams.DBPath) && Directory.Exists(GlobalParams.DefaultBackupPath()))
            {
                _isAppCheckPass = false;
                MessageBox.Show("The Database does Not Exist!" + "\n" + "Please check your Database file or contect your I.T. support.", "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
                //if the system checking fail, exit application
                //Application.Exit();
            }
            else
            {
                _isAppCheckPass = false;
                MessageBox.Show("Oops! It looks like the System structure is broken!" +
                    "\nPlease try to reinstall it or contect your I.T. support." +
                    "\nIf you have data in your Databse, Please contect your I.T. support.", "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
                //if the system checking fail, exit application
               //Application.Exit();
            }
        }
        #endregion
    }
}
